//
//  SMPrdDetailViewController.m
//  ShoppingMall
//
//  Created by Demon on 2018/3/22.
//  Copyright © 2018年 Demon. All rights reserved.
//

#import "SMPrdDetailViewController.h"
#import "SMPrdDetailHeadCell.h"
#import "SMHomeTableViewCell.h"
#import "SMProductModel.h"
#import "SMPrdDetailViewController.h"
#import "YDWebViewController.h"

@interface SMPrdDetailViewController () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *detailHomeTablView;
@property (nonatomic, strong) SMProductModel *prdModel;
@property (nonatomic, strong) NSArray<SMProductModel *> *prdArray;

@end

@implementation SMPrdDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.detailHomeTablView registerNib:[UINib nibWithNibName:[SMPrdDetailHeadCell className] bundle:nil] forCellReuseIdentifier:@"SMPrdDetailHeadCell"];
    [self.detailHomeTablView registerNib:[UINib nibWithNibName:[SMHomeTableViewCell className] bundle:nil] forCellReuseIdentifier:@"SMHomeTableViewCell"];
    
    [[YDNetClient sharedInstance] getMethod:@"http://jidianping.com/api/tb/datadetail.php" param:@{@"pid":self.prdId} success:^(id object) {
        self.prdModel = [SMProductModel mj_objectWithKeyValues:object[@"product"]];
        self.prdArray = [SMProductModel mj_objectArrayWithKeyValuesArray:object[@"listData"]];
        [self.detailHomeTablView reloadData];
    } fail:^(NSError *error) {
        
    }];
}

- (IBAction)applyAndShare:(UIButton *)sender {
    YDWebViewController *webVC = ALLOC(YDWebViewController);
    webVC.webUrl = self.prdModel.couponurl;
    [self.navigationController pushViewController:webVC animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        YDWebViewController *webVC = ALLOC(YDWebViewController);
        webVC.webUrl = self.prdModel.producturl;
        webVC.navigationItem.title = @"商品";
        [self.navigationController pushViewController:webVC animated:YES];
    } else if (indexPath.section == 1) {
        SMPrdDetailViewController *detailVC = MainStoryBoardVC([SMPrdDetailViewController className]);
        SMProductModel *prdModel = self.prdArray[indexPath.row];
        detailVC.prdId = prdModel.pid;
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) return self.prdModel ? 1 : 0;
    return self.prdArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        SMPrdDetailHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:[SMPrdDetailHeadCell className] forIndexPath:indexPath];
        [cell setDetailHeadCellModel:self.prdModel];
        @weakify(self)
        cell.headCellClick = ^{
            @strongify(self)
            YDWebViewController *webVC = ALLOC(YDWebViewController);
            webVC.webUrl = self.prdModel.couponurl;
            webVC.navigationItem.title = @"抢券";
            [self.navigationController pushViewController:webVC animated:YES];
        };
        return cell;
    } else {
        SMHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SMHomeTableViewCell" forIndexPath:indexPath];
        cell.indexPathCell = indexPath;
        SMProductModel *model = self.prdArray[indexPath.row];
        [cell setHomeTableViewModel:model];
        cell.shareBtnClick = ^(NSIndexPath *indexPathCell) {
            [self tableView:tableView didSelectRowAtIndexPath:indexPathCell];
        };
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) return ALLOC(UIView);
    UIView *view = ALLOC(UIView);
    UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 39)];
    view.backgroundColor = UIColorHex(ffffff);
    lb.textColor = UIColorHex(EA7625);
    lb.font = UIFONTSIZE(16);
    lb.text = @"相似商品";
    lb.textAlignment = NSTextAlignmentCenter;
    UILabel *lines = [[UILabel alloc] initWithFrame:CGRectMake(0, 39.5, kScreenW, 0.5)];
    lines.backgroundColor = UIColorHex(eaeaea);
    [view addSubview:lines];
    [view addSubview:lb];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) return 0.000001;
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return ALLOC(UIView);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.000001;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
